<!doctype html>
<html lang="en-US">
  <head>
    <link href="/assets/index.css" rel="stylesheet" type="text/css" />
    <link href="/assets/accessibility.liveRegionAttachment.css" rel="stylesheet" type="text/css" />
    <script crossorigin="anonymous" src="/test-harness.js"></script>
    <script crossorigin="anonymous" src="/test-page-object.js"></script>
    <script crossorigin="anonymous" src="/__dist__/webchat-es5.js"></script>
    <style>
      .webchat__submit-error-message {
        all: unset !important;
        bottom: 60px !important;
        color: darkred !important;
        margin: 5px !important;
        position: absolute !important;
      }
      .webchat__connectivityStatus > div {
        background-image: none !important;
      }
    </style>
  </head>
  <body>
    <main id="webchat"></main>
    <script>
      run(async function () {
        const clock = lolex.createClock();

        const { directLine, store } = testHelpers.createDirectLineEmulator({ autoConnect: false, ponyfill: clock });

        WebChat.renderWebChat(
          {
            directLine,
            ponyfill: clock,
            store,
            styleOptions: {
              internalLiveRegionFadeAfter: 10000
            }
          },
          document.getElementById('webchat')
        );

        await pageConditions.webChatRendered();

        // GIVEN: Connection is still establishing.
        expect(pageElements.connectivityStatus().innerText).toBe('Connecting…');
        await host.snapshot('local');

        const errorMessage = document.querySelector('.webchat__submit-error-message');

        // GIVEN: "Hello, World!" is in the send box.
        await pageObjects.typeInSendBox('Hello, World!');

        // WHEN: Send button is clicked.
        await pageObjects.clickSendButton();

        // THEN: The message "Unable to connect." should appear.
        expect(errorMessage.innerText).toBe('Unable to connect.');
        await host.snapshot('local');

        // THEN: After 10000 ms, the error message should still be visible.
        clock.tick(10000);
        expect(errorMessage.innerText).toBe('Unable to connect.');
        await host.snapshot('local');

        // WHEN: Send button is clicked again.
        await pageObjects.clickSendButton();

        // THEN: The error message should still be "Unable to connect."
        expect(errorMessage.innerText).toBe('Unable to connect.');
        await host.snapshot('local');
      });
    </script>
  </body>
</html>
